package com.qdxwx.data.service.user;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.data.mapper.user.UserCountMapper;
import com.qdxwx.models.user.UserCount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class UserCountServiceImpl implements UserCountService {
    @Autowired
    UserCountMapper userCountMapper;

    @Override
    public UserCount get(long userId) {
        if (userId < 1) {
            throw new RuntimeException("UserCount.get userId is " + userId);
        }
        UserCount count = userCountMapper.get(userId);
        if (count == null) {
            count = new UserCount();
            count.setId(userId);
            insert(count);
            count.initAll();
        } else {
            if (!count.getUpdateTime().toLocalDate().isEqual(LocalDate.now())) {
                if (count.getUpdateTime().getMonthValue() != LocalDate.now().getMonthValue()) {
                    count.initMonthAll();
                }
                count.initDayAll();
                count.setUpdateTime(LocalDateTime.now());
                userCountMapper.update(count);
            }
        }
        return count;
    }

    @Override
    public int insert(UserCount userCount) {
        userCount.setUpdateTime(LocalDateTime.now());
        return userCountMapper.insert(userCount);
    }

    @Override
    public int append(UserCount userCount) {
        if (userCount.getId() < 1) return 0;
        userCount.setUpdateTime(LocalDateTime.now());
        return userCountMapper.append(userCount);
    }

    @Override
    public void updateCourseDownCount(long userId, int number) {
        UserCount userCount = new UserCount();
        userCount.setId(userId);
        userCount.setCourseDownDay(number);
        append(userCount);
    }

    @Override
    public void updatePictureDownCount(long userId, int number) {
        UserCount userCount = new UserCount();
        userCount.setId(userId);
        userCount.setPictureDownDay(number);
        append(userCount);
    }

    @Override
    public void updateDocumentDownCount(long userId, int number) {
        UserCount userCount = new UserCount();
        userCount.setId(userId);
        userCount.setDocumentDownDay(number);
        append(userCount);
    }

    @Override
    public void updatePictureUploadCount(long userId, int number) {
        UserCount userCount = new UserCount();
        userCount.setId(userId);
        userCount.setPictureUploadDay(number);
        append(userCount);
    }


    @Override
    public AjaxResult checkPictureLimit(long userId, boolean isVip) {
        if (userId <= 0) {
            return AjaxResult.error("用户Id错误！");
        }
        int limitDay = isVip ? 100 : 50;
        //判断下载份数
        UserCount userCount = get(userId);
        if (userCount.getPictureDownDay() >= limitDay) {
            if (isVip) {
                return AjaxResult.error("每日下载素材限额 " + limitDay + " 份，请明天再来。");
            } else {
                return AjaxResult.error("普通用户下载素材限额 " + limitDay + " 份，升级VIP可提升限额。");
            }
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult checkDocumentLimit(long userId, boolean isVip) {
        if (userId <= 0) {
            return AjaxResult.error("用户Id错误！");
        }
        int limitDay = isVip ? 100 : 50;
        //判断下载份数
        UserCount userCount = get(userId);
        if (userCount.getDocumentDownDay() >= limitDay) {
            if (isVip) {
                return AjaxResult.error("每日下载限额 " + limitDay + " 份，请明天再来。");
            } else {
                return AjaxResult.error("普通用户下载限额 " + limitDay + " 份，升级VIP可提升限额。");
            }
        }
        return AjaxResult.success();
    }

    @Override
    public int update(UserCount userCount) {
        return userCountMapper.update(userCount);
    }

    @Override
    public int delete(long userId) {
        return userCountMapper.deleteById(userId);
    }

    @Override
    public int updateCombine(long userId, UserCount userCount) {
        if(userId < 1 || userCount == null){
            throw new RuntimeException("账户合并：参数异常");
        }
        return userCountMapper.updateCombine(userId, userCount);
    }
}
